function UI_CreateWindow ( title, x, y, w, h )
	local ele = createElement ( "UI_Window" )
	setElementParent ( ele, getResourceDynamicElementRoot ( sourceResource or getThisResource ( ) ) )
	
	setElementData ( ele, "UI_Title", title )
	setElementData ( ele, "UI_X", x )
	setElementData ( ele, "UI_Y", y )
	setElementData ( ele, "UI_Width", w )
	setElementData ( ele, "UI_Height", h )
	setElementData ( ele, "UI_Movable", true )
	setElementData ( ele, "UI_Alpha", 255 )
	setElementData ( ele, "UI_Visible", true )
	setElementData ( ele, "UI_RGB", { ["Red"] = 255, ["Green"] = 255, ["Blue"] = 255 } )
	setElementData ( ele, "UI_Font", "default-bold" )
	setElementData ( ele, "UI_FontSize", 1 )
	setElementData ( ele, "UI_ColorCoded", false )
	setElementData ( ele, "UI_Theme", UI_ThemeDefault )
	setElementData ( ele, "UI_EventMouseState", false )
	
	setElementData ( ele, "UI_RenderTarget", dxCreateRenderTarget ( w > 1 and w or 1, h > 1 and h or 1, true ) )

	w = w - UI_ThemeGetWidth ( "Frame2Left", UI_ThemeDefault ) - UI_ThemeGetWidth ( "Frame2Right", UI_ThemeDefault )
	h = h - UI_ThemeGetHeight ( "TitleBarBackground", UI_ThemeDefault ) - UI_ThemeGetHeight ( "Frame2Bottom", UI_ThemeDefault )
	setElementData ( ele, "UI_RenderTarget2", dxCreateRenderTarget ( w > 1 and w or 1, h > 1 and h or 1 ) )
	
	table.insert ( UI_Render, ele )
	return ele
end

function UI_WindowGetTitle ( ele )
	if ( UI_isElement ( ele ) and getElementType ( ele ) == "UI_Window" )  then
		return getElementData ( ele, "UI_Title" )
	end
	return false
end

function UI_WindowSetTitle ( ele, title )
	if ( UI_isElement ( ele ) and getElementType ( ele ) == "UI_Window" )  then
		if ( setElementData ( ele, "UI_Title", title ) ) then
			return true
		end
	end
	return false
end

function UI_WindowGetMovable ( ele )
	if ( UI_isElement ( ele ) and getElementType ( ele ) == "UI_Window" )  then
		return getElementData ( ele, "UI_Movable" )
	end
	return false
end

function UI_WindowSetMovable ( ele, toggle )
	if ( UI_isElement ( ele ) and getElementType ( ele ) == "UI_Window" )  then
		if ( setElementData ( ele, "UI_Movable", toggle ) ) then
			return true
		end
	end
	return false
end

function UI_RenderWindow ( ele )
	local Title = getElementData ( ele, "UI_Title" ) or " "
	
	local X = getElementData ( ele, "UI_X" ) or 0
	local Y = getElementData ( ele, "UI_Y" ) or 0
	local Width = getElementData ( ele, "UI_Width" ) or 0
	local Height = getElementData ( ele, "UI_Height" ) or 0
	local Movable = getElementData ( ele, "UI_Movable" )
	local RenderTarget = getElementData ( ele, "UI_RenderTarget" )
	local RenderTarget2 = getElementData ( ele, "UI_RenderTarget2" )
	local Alpha = getElementData ( ele, "UI_Alpha" ) or 255
	local RGB = getElementData ( ele, "UI_RGB" ) or { ["Red"] = 255, ["Green"] = 255, ["Blue"] = 255 }
	local Font = getElementData ( ele, "UI_Font" ) or "default-bold"
	local FontSize = getElementData ( ele, "UI_FontSize" ) or 1
	if ( isElement ( Font ) and getElementType ( Font ) == "UI_Font" ) then
		Font = UI_TitleFont[ele]
	end
	local Colored = getElementData ( ele, "UI_ColorCoded" ) or false
	local Theme = getElementData ( ele, "UI_Theme" ) or UI_ThemeDefault
	local ThemeXPos = 0
	local ThemeYPos = 0
	local ThemeWidth = 0
	local ThemeHeight = 0
	local ThemeImage = ""
	local Images = {}
	local Selected = ( isElement ( UI_WindowSelected ) and ele == UI_WindowSelected ) and "Selected" or ""

	if ( UI_RenderParent[ele] ) then
		for _, v in ipairs ( UI_RenderParent[ele] ) do
			if ( UI_GetVisible ( v ) ) then
				local ParentX = getElementData ( v, "UI_X" ) or 0
				local ParentY = getElementData ( v, "UI_Y" ) or 0
				local ParentWidth = getElementData ( v, "UI_Width" ) or 0
				local ParentHeight = getElementData ( v, "UI_Height" ) or 0
				if ( getElementType ( v ) == "UI_Button" ) then
					table.insert ( Images, { ["Element"] = v, ["Type"] = "Button", ["X"] = ParentX, ["Y"] = ParentY, ["Width"] = ParentWidth, ["Height"] = ParentHeight, ["Image"] = UI_ImageGeneratorButton ( v ) } )
				elseif ( getElementType ( v ) == "UI_Label" ) then
					table.insert ( Images, { ["Element"] = v, ["Type"] = "Label", ["X"] = ParentX, ["Y"] = ParentY, ["Width"] = ParentWidth, ["Height"] = ParentHeight, ["Image"] = UI_ImageGeneratorLabel ( v ) } )
				elseif ( getElementType ( v ) == "UI_CheckBox" ) then
					table.insert ( Images, { ["Element"] = v, ["Type"] = "CheckBox", ["X"] = ParentX, ["Y"] = ParentY, ["Width"] = ParentWidth, ["Height"] = ParentHeight, ["Image"] = UI_ImageGeneratorCheckBox ( v ) } )
				elseif ( getElementType ( v ) == "UI_RadioButton" ) then
					table.insert ( Images, { ["Element"] = v, ["Type"] = "RadioButton", ["X"] = ParentX, ["Y"] = ParentY, ["Width"] = ParentWidth, ["Height"] = ParentHeight, ["Image"] = UI_ImageGeneratorRadioButton ( v ) } )
				elseif ( getElementType ( v ) == "UI_ProgressBar" ) then
					table.insert ( Images, { ["Element"] = v, ["Type"] = "ProgressBar", ["X"] = ParentX, ["Y"] = ParentY, ["Width"] = ParentWidth, ["Height"] = ParentHeight, ["Image"] = UI_ImageGeneratorProgressBar ( v ) } )
				end
			end
		end
	end

	dxSetRenderTarget ( RenderTarget2, true )
	ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage = UI_ThemeGetAll ( "Frame2Background", Theme )
	dxDrawImageSection ( 0, 0, Width - UI_ThemeGetWidth ( "Frame2Left", Theme ) - UI_ThemeGetWidth ( "Frame2Right", Theme ), Height - UI_ThemeGetHeight ( "TitleBarBackground" .. Selected, Theme ) - UI_ThemeGetHeight ( "Frame2Bottom", Theme ), ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage )
	for _, v in ipairs ( Images ) do
		local alpha = UI_GetAlpha ( v["Element"] ) or 255
		local color = tocolor ( 255, 255, 255, alpha )
		if ( getElementType ( v["Element"] ) == "UI_Button" ) then
			color = tocolor ( 255, 255, 255, 204 )
			if ( getElementData ( v["Element"], "UI_EventMouseState" ) or UI_Selected == v["Element"] ) then
				color = tocolor ( 255, 255, 255, alpha )
			end
		end
		dxDrawImage ( v["X"], v["Y"], v["Width"], v["Height"], v["Image"], 0, 0, 0, color )
	end
	dxSetRenderTarget ( )

	dxSetRenderTarget ( RenderTarget, true )
	dxDrawImage ( UI_ThemeGetWidth ( "Frame2Left", Theme ), UI_ThemeGetHeight ( "TitleBarBackground", Theme ), Width - UI_ThemeGetWidth ( "Frame2Left", Theme ) - UI_ThemeGetWidth ( "Frame2Right", Theme ), Height - UI_ThemeGetHeight ( "TitleBarBackground", Theme ) - UI_ThemeGetHeight ( "Frame2Bottom", Theme ), RenderTarget2 )
	
	ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage = UI_ThemeGetAll ( "Frame2Left", Theme )
	dxDrawImageSection ( 0, UI_ThemeGetHeight ( "TitleBarTopLeft" .. Selected, Theme ), ThemeWidth, Height - UI_ThemeGetHeight ( "TitleBarTopLeft" .. Selected, Theme ) - UI_ThemeGetHeight ( "Frame2BottomLeft", Theme ), ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage )
	ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage = UI_ThemeGetAll ( "Frame2Right", Theme )
	dxDrawImageSection ( Width - ThemeWidth, UI_ThemeGetHeight ( "TitleBarTopRight" .. Selected, Theme ), ThemeWidth, Height - UI_ThemeGetHeight ( "TitleBarTopLeft" .. Selected, Theme ) - UI_ThemeGetHeight ( "Frame2BottomLeft", Theme ), ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage )
	ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage = UI_ThemeGetAll ( "Frame2Bottom", Theme )
	dxDrawImageSection ( UI_ThemeGetWidth ( "Frame2BottomLeft", Theme ), Height - ThemeHeight, Width - UI_ThemeGetWidth ( "Frame2BottomLeft", Theme ) - UI_ThemeGetWidth ( "Frame2BottomRight", Theme ), ThemeHeight, ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage )
	ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage = UI_ThemeGetAll ( "Frame2BottomLeft", Theme )
	dxDrawImageSection ( 0, Height - ThemeHeight, ThemeWidth, ThemeHeight, ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage )
	ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage = UI_ThemeGetAll ( "Frame2BottomRight", Theme )
	dxDrawImageSection ( Width - ThemeWidth, Height - ThemeHeight, ThemeWidth, ThemeHeight, ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage )
	ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage = UI_ThemeGetAll ( "TitleBarTop" .. Selected, Theme )
	dxDrawImageSection ( UI_ThemeGetWidth ( "TitleBarTopLeft" .. Selected, Theme ), 0, Width - UI_ThemeGetWidth ( "TitleBarTopLeft" .. Selected, Theme ) - UI_ThemeGetWidth ( "TitleBarTopRight" .. Selected, Theme ), ThemeHeight, ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage )
	ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage = UI_ThemeGetAll ( "TitleBarTopLeft" .. Selected, Theme )
	dxDrawImageSection ( 0, 0, ThemeWidth, ThemeHeight, ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage )
	ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage = UI_ThemeGetAll ( "TitleBarTopRight" .. Selected, Theme )
	dxDrawImageSection ( Width - ThemeWidth, 0, ThemeWidth, ThemeHeight, ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage )
	ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage = UI_ThemeGetAll ( "TitleBarBackground" .. Selected, Theme )
	dxDrawImageSection ( UI_ThemeGetWidth ( "TitleBarTopLeft" .. Selected, Theme ), 0, Width - UI_ThemeGetWidth ( "TitleBarTopLeft" .. Selected, Theme ) - UI_ThemeGetWidth ( "TitleBarTopRight" .. Selected, Theme ), ThemeHeight, ThemeXPos, ThemeYPos, ThemeWidth, ThemeHeight, ThemeImage, 0, 0, 0, tocolor ( 255, 255, 255, 255 ) )
	dxDrawText ( Title, Width / 2, UI_ThemeGetHeight ( "TitleBarBackground" .. Selected, Theme ) / 2, tocolor ( RGB["Red"], RGB["Green"], RGB["Blue"], 255 ), FontSize, Font, "center", "center", _, Colored )
	dxSetRenderTarget ( )

	dxDrawRectangle ( X, Y + UI_ThemeGetHeight ( "TitleBarBackground" .. Selected, Theme ) - 1, Width, 1, tocolor ( 0, 0, 0, Alpha ) )
	dxDrawImage ( X, Y, Width, Height, RenderTarget, 0, 0, 0, tocolor ( 255, 255, 255, Alpha ) )

	if ( isElement ( UI_IsMoving ) and UI_IsMoving == ele and Movable ) then
		local cX, cY = getCursorPosition ( )
		cX = cX * ScreenX
		cY = cY * ScreenY
		
		setElementData ( ele, "UI_X", cX + UI_Offset["X"] )
		setElementData ( ele, "UI_Y", cY + UI_Offset["Y"] )
	end
end